package com.situ.object.dao.impl;



import com.situ.object.common.Page;
import com.situ.object.dao.CourseDao;
import com.situ.object.entity.Course;
import com.situ.object.utils.DBUtil;
import com.situ.object.vo.CourseVo;

import java.util.List;

public class CourseDaoImpl implements CourseDao {
    private DBUtil dbUtil = new DBUtil();


    @Override
    public int add(Course course) {
        String sql = "INSERT INTO manage_system.course(courseName, studentId, courseCredit, type) VALUES (?,?,?,?)";
        return dbUtil.update(sql,course.getCourseName(),course.getStudentId(),course.getCourseCredit(),course.getType());
    }

    @Override
    public int delete(Integer id) {
        String sql = "update course set status = 1 where courseId = ? ";
        return dbUtil.update(sql,id);
    }

    @Override
    public int update(Course course) {
        String sql = "UPDATE manage_system.course SET courseName = ?, studentId = ?, courseCredit = ?, type = ?, status = ? WHERE courseId = ? ";
        return dbUtil.update(sql,course.getCourseName(),course.getStudentId(),course.getCourseCredit(),course.getType(),course.getStatus(),course.getCourseId());
    }

    @Override
    public Course selectById(Course course) {
        String sql = "select * from course where courseId = ?";
        return dbUtil.queryOne(Course.class,sql,course.getCourseId());
    }

    @Override
    public List<Course> selectAll() {
        String sql = "select * from course ";
        return dbUtil.query(Course.class,sql);
    }

    @Override
    public Long selectCount(String sql,List<Object> params) {
        String sqlCount = "select count(*) as count from ("+ sql +") as t" ;
        return dbUtil.queryOne(Page.class,sqlCount,params.toArray()).getCount();
    }

    @Override
    public List selectList(Integer page, Integer limit,String sql,List<Object> params) {
        int m = (page - 1) * limit;
        String sqlList = "select * from ("+ sql +") as t limit ?,?";
        params.add(m);
        params.add(limit);
        System.out.println("page"+page);
        System.out.println("limit"+limit);
        return dbUtil.query(CourseVo.class,sqlList,params.toArray());
    }
}
